﻿#region Using Statements
using DirectZZT.Gameplay;
using DirectZZT.Shared.Core;
using DirectZZT.Shared.World;
using DirectZZT.Shared.World.Objects;
#endregion

namespace DirectZZT.BoardObjects
{
    /// <summary>
    /// Represents the gem ("Gem") board object.
    /// </summary>
    [Export("Gem")]
    [BoardObject(Identifier = "Gem", Classification = BoardObjectClassification.Item, AllowBecome = false)]
    public sealed class GemBoardObject : BoardObject, IItemBoardObject
    {
        #region Constants

        /// <summary>
        /// Defines the amount of gemes this item adds.
        /// </summary>
        private const int GemAmount = 1;

        #endregion

        #region Fields

        private int _incrementAmount = GemAmount;

        #endregion

        #region Properties

        /// <summary>
        /// Gems can be moved and has collision.
        /// </summary>
        public override BoardObjectCapabilities Capabilities
        {
            get { return base.Capabilities /* | BoardObjectCapabilities.Movable*/; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the GemBoardObject class.
        /// </summary>
        public GemBoardObject()
        {
            this.Texture = "Textures/PlayfieldObjects/Gem";
            this.Enabled = true;
            this.Visible = true;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Only DirectZZT supported.
        /// </summary>
        /// <param name="featureSet"></param>
        /// <returns></returns>
        protected override bool CanAttachScript(FeatureSet featureSet)
        {
            return featureSet == FeatureSet.DirectZZT;
        }

        /// <summary>
        /// Called when this object spawns.
        /// </summary>
        /// <param name="modifiers"></param>
        public override void OnSpawn(string[] modifiers)
        {
            base.OnSpawn(modifiers);

            // read out modifiers, if allowed for this item
            if (this.Board.World.FeatureSet == FeatureSet.DirectZZT && (modifiers != null && modifiers.Length == 1))
            {
                _incrementAmount = modifiers[0].TryGetInt32(GemAmount);
                // no negative values!
                if (_incrementAmount < 0)
                {
                    _incrementAmount = GemAmount;
                }
            }
        }

        #endregion

        #region IItemBoardObject Members

        bool IItemBoardObject.CanPickup(BoardObject obj)
        {
            // only a player can pick us up
            return obj is IPlayerBoardObject;
        }

        void IItemBoardObject.OnPickup(BoardObject obj)
        {
            IPlayerBoardObject player = (IPlayerBoardObject)obj;
            // add gems
            player.Player.Inventory.GemAmount += _incrementAmount;
            // also for each gem, increment the player's health
            player.Player.Health = CreatureTools.SetHealth(++player.Player.Health);
        }

        #endregion
    }
}
